package com.testing.sunnycommon.controller;

import com.testing.sunnycommon.mapper.SunnyCaseMapper;
import com.testing.sunnycommon.model.Result;
import com.testing.sunnycommon.model.SunnyCase;
import com.testing.sunnycommon.service.SunnyCaseService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @Classname SunnyCaseController
 * @Description 类型说明
 * @Date 2023/2/20 22:14
 * @Created by 特斯汀Roy
 */
@Api(value = "用例设计接口", tags = "用例设计接口", description = "用例相关操作接口")
@RestController
@RequestMapping("/sunnyCase")
public class SunnyCaseController {

    @Autowired
    SunnyCaseMapper sunnyCaseMapper;

    @Autowired
    SunnyCaseService sunnyCaseService;

//    @Autowired
//    SunnyProjectService sunnyProjectService;


    @ApiOperation("列出所有的测试用例")
    @ApiResponses({
            @ApiResponse(code = 1000, message = "查询成功")
    })
    @GetMapping
    public Result<List<SunnyCase>> listCases() {
        List<SunnyCase> cases = sunnyCaseMapper.getCases();
        System.out.println("查到的用例是" + cases);
        return Result.success(cases);
    }

    @GetMapping("/view")
    public ModelAndView showCases() {
        ModelAndView caseView = new ModelAndView();
        List<SunnyCase> cases = sunnyCaseMapper.getCases();
        //注意attributeName和template中的变量名是对应的
        caseView.addObject("sunnyCases", cases);
        caseView.setViewName("sunnyCase");
        return caseView;
    }

    @ApiOperation("获取模块中的测试用例")
    @GetMapping("/inModule/{moduleId}")
    public Result<List<SunnyCase>> getCasesInModule(@PathVariable("moduleId") Long moduleId) {
        List<SunnyCase> casesInModule = sunnyCaseService.getCasesInModule(moduleId);
        return Result.success(casesInModule);
    }

    @ApiOperation("添加测试用例")
    @ApiResponses({
            @ApiResponse(code = 1000, message = "新增成功",
                    examples = @Example(
                            @ExampleProperty("{\"code\":1000,\"msg\":\"新增用例成功\"}"))),
            @ApiResponse(code = 1010, message = "新增失败")
    })
    @PostMapping
    public Result addCase(@ApiParam("需要添加的测试用例的对象Json") @RequestBody SunnyCase sunnyCase) {
        System.out.println(sunnyCase);
        int i = sunnyCaseMapper.addCase(sunnyCase);
//        sunnyProjectService.list().forEach(project -> sunnyProjectService.updateSunnyProject(project));

        System.out.println("执行操作的结果是：" + i);
        if (i > 0) {
            return Result.success(sunnyCase);
        } else {
            return Result.fail(1010, "新增用例失败", sunnyCase);
        }
    }

    //通过批量处理完成更新的操作。
    @ApiOperation("修改测试用例")
    @PutMapping
    public Result updateCase(@RequestBody List<SunnyCase> sunnyCaseList) {
        int i = sunnyCaseMapper.updateCaseBatch(sunnyCaseList);
        if (i > 0)
            return Result.success("修改用例成功");
        else
            return Result.fail(1030, "修改测试用例失败", sunnyCaseList);
    }


    @ApiOperation("更新所有测试用例的作者")
    @PutMapping("/author")
    public Result updateAuthor(@ApiParam("作者名") @RequestParam String author) {
        int i = sunnyCaseMapper.upDateAuthor(author);
        System.out.println("更新的结果是:" + i);
        return Result.success("更新作者成功");
    }

    @ApiOperation("基于id删除一条测试用例")
    @DeleteMapping("/{id}")
    public Result deleteCaseById(@ApiParam("要删除的用例的id") @PathVariable Long id) {
        System.out.println(id);
        int i = sunnyCaseMapper.deleteCase(id);
        System.out.println("删除的结果是：" + i);
        if (i > 0) {
            return Result.success("删除用例陈宫");
        } else {
            return Result.fail(1020, "删除用例失败", "删除用例失败");
        }
    }

    @ApiOperation("基于id列表进行用例删除")
    @DeleteMapping("/ids/{ids}")
    public Result deleteCases(@ApiParam("要删除的用例的id列表，多个以逗号隔开") @PathVariable List<Long> ids) {
//        Long aLong = ids.get(2);

        //放到controller层处理也是OK的
//        //遍历数组完成删除。
//        int delCount=0;
//        for (Long id : ids) {
//            delCount+= sunnyCaseMapper.deleteCase(id);
//        }
//        System.out.println("删除了"+delCount+"条用例");
//        return delCount;

        //符合设计理念的是放到service处理业务逻辑。
        int delCount = sunnyCaseService.deleteCases(ids);

//        sunnyProjectService.list().forEach(project -> sunnyProjectService.updateSunnyProject(project));

        if (delCount > 0) {
            return Result.success("删除成功");
        } else {
            return Result.fail(1020, "删除失败", ids);
        }
    }

}
